package com.cetc.nsfx.controller.sys;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cetc.nsfx.security.vo.UserContext;
import com.cetc.nsfx.service.sys.UserService;
import com.cetc.nsfx.util.EncryptUtils;

/**
 * Created by Administrator on 2016/1/15 0015.
 */
@Controller
@RequestMapping("/sys/user")
public class SysUserController {

	@Autowired
	UserService userService;
	
    @RequestMapping("/toUserList")
	public String toUserList(){
		return "/sys/userList";
	}
    
    @RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request) {
		
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		int iDisplayLength = Integer.valueOf(request.getParameter("iDisplayLength"));	
		int iDisplayStart = Integer.valueOf(request.getParameter("iDisplayStart"));
		
		String sSearch = request.getParameter("sSearch");
		String searchName = "";
		try {
			searchName = new String(sSearch.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String loginName = user.getUserId();
		Map<String,Object> userMap = userService.loadUser(loginName,searchName,iDisplayStart,iDisplayLength);
		Map<String,Object> userData = new HashMap<String,Object>();
		userData.put("iTotalRecords",userMap.get("count"));
		userData.put("iTotalDisplayRecords", userMap.get("count"));
		userData.put("aaData", userMap.get("rs"));
		return userData;	
	}
    
    @RequestMapping(value = "resetpwd", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> resetpwd(HttpServletRequest request,HttpServletResponse respose) {
		
		Map<String,Object> map=new HashMap<String,Object>();
		String loginName = request.getParameter("loginName");
		boolean result= userService.resetpwd(loginName);
		if(result){
			map.put("success", "success");
		}else{
			map.put("success", "failed");
		}
		return map;
	}
}
